package admin.common.vo;

import admin.common.enums.ResponseEnum;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
import lombok.experimental.SuperBuilder;

import java.io.Serializable;

/**
 * @author zgb
 * @date 2024/1/16
 * @description TODO
 */
@Schema(description = "响应对象")
@Data
@SuperBuilder
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
public class ResponseVO<T> implements Serializable {

    private static final long serialVersionUID = -1242493306307174690L;


    /**
     * 响应码
     */
    @Schema(description = "响应码0成功，其他失败")
    private Integer code;

    /**
     * 响应信息
     */
    @Schema(description = "响应信息")
    private String msg;

    /**
     * 数据
     */
    @Schema(description = "数据")
    private T data;


    public static <T> ResponseVO<T> OK() {
        return rspMsg(ResponseEnum.SUCCESS);
    }

    public static <T> ResponseVO<T> error() {
        return rspMsg(ResponseEnum.SERVER_INNER_ERR);
    }

    public static <T> ResponseVO<T> error(String msg) {
        ResponseVO<T> message = new ResponseVO<T>();
        message.setCode(ResponseEnum.SERVER_INNER_ERR.getCode());
        message.setMsg(msg);
        return message;
    }

    public static <T> ResponseVO<T> error(Integer code, String msg) {
        ResponseVO<T> message = new ResponseVO<T>();
        message.setCode(code);
        message.setMsg(msg);
        return message;
    }


    public static <T> ResponseVO<T> rspMsg(ResponseEnum responseEnum) {
        ResponseVO<T> message = new ResponseVO<T>();
        message.setCode(responseEnum.getCode());
        message.setMsg(responseEnum.getMsg());
        return message;
    }

    public static <T> ResponseVO<T> rspMsg(ResponseEnum responseEnum, T data) {
        ResponseVO<T> message = new ResponseVO<T>();
        message.setCode(responseEnum.getCode());
        message.setMsg(responseEnum.getMsg());
        message.setData(data);
        return message;
    }


    /**
     * 成功
     *
     * @param data 数据
     * @param <T>  泛型
     */
    public static <T> ResponseVO<T> OK(T data) {
        ResponseVO<T> responseData = new ResponseVO<T>();
        responseData.setCode(ResponseEnum.SUCCESS.getCode());
        responseData.setData(data);
        responseData.setMsg(ResponseEnum.SUCCESS.getMsg());
        return responseData;
    }


    public static <T> ResponseVO<T> OK(Integer code, T data) {
        ResponseVO<T> responseData = new ResponseVO<T>();
        responseData.setCode(code);
        responseData.setData(data);
        return responseData;
    }

    public static ResponseVO feiginError() {
        return ResponseVO.builder().code(400).msg("feign失败").build();
    }


    /**
     * 成功返回结果
     */
    public static <T> ResponseVO<T> success() {
        return new ResponseVO<T>(ResponseEnum.SUCCESS.getCode(), ResponseEnum.SUCCESS.getMsg(), null);
    }

    public static <T> ResponseVO<T> success(T data) {
        return new ResponseVO<>(ResponseEnum.SUCCESS.getCode(), ResponseEnum.SUCCESS.getMsg(), data);
    }

    /**
     * 成功返回结果
     *
     * @param data 获取的数据
     * @param msg  提示信息
     */
    public static <T> ResponseVO<T> success(T data, String msg) {
        return new ResponseVO<T>(ResponseEnum.SUCCESS.getCode(), msg, data);
    }

    /**
     * 失败返回结果
     *
     * @param responseEnum 错误码
     */
    public static <T> ResponseVO<T> failed(ResponseEnum responseEnum) {
        return new ResponseVO<T>(responseEnum.getCode(), responseEnum.getMsg(), null);
    }

    /**
     * 失败返回结果
     *
     * @param responseEnum 错误码
     * @param msg          错误信息
     */
    public static <T> ResponseVO<T> failed(ResponseEnum responseEnum, String msg) {
        return new ResponseVO<T>(responseEnum.getCode(), msg, null);
    }

    /**
     * 失败返回结果
     *
     * @param msg 提示信息
     */
    public static <T> ResponseVO<T> failed(String msg) {
        return new ResponseVO<T>(ResponseEnum.FAILED.getCode(), msg, null);
    }

    /**
     * 失败返回结果
     */
    public static <T> ResponseVO<T> failed() {
        return failed(ResponseEnum.FAILED);
    }

    /**
     * 参数验证失败返回结果
     */
    public static <T> ResponseVO<T> validateFailed() {
        return failed(ResponseEnum.VALIDATE_FAILED);
    }

    /**
     * 参数验证失败返回结果
     *
     * @param msg 提示信息
     */
    public static <T> ResponseVO<T> validateFailed(String msg) {
        return new ResponseVO<T>(ResponseEnum.VALIDATE_FAILED.getCode(), msg, null);
    }

    /**
     * 未登录返回结果
     */
    public static <T> ResponseVO<T> unauthorized(T data) {
        return new ResponseVO<T>(ResponseEnum.UNAUTHORIZED.getCode(), ResponseEnum.UNAUTHORIZED.getMsg(), data);
    }

    /**
     * 未授权返回结果
     */
    public static <T> ResponseVO<T> forbidden(T data) {
        return new ResponseVO<T>(ResponseEnum.FORBIDDEN.getCode(), ResponseEnum.FORBIDDEN.getMsg(), data);
    }

}
